package com.wicket.okrfinance.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrfinance.base.service.MOmsTargetCostsetService;
import com.wicket.okrfinance.base.service.dto.req.*;
import com.wicket.okrfinance.common.dto.FieldInfo;
import com.wicket.okrfinance.common.exception.BizException;
import com.wicket.okrfinance.common.tools.StringUtils;
import com.wicket.okrfinance.common.util.CommonFunctionHelper;
import com.wicket.okrfinance.dal.mapper.mbg.OmsTargetCostsetMapper;
import com.wicket.okrfinance.dal.po.mbg.OmsAccount;
import com.wicket.okrfinance.dal.po.mbg.OmsTargetCostset;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
//import com.wicket.okrfinance.base.service.dto.res.*;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsTargetCostsetServiceImpl
  extends ServiceImpl<OmsTargetCostsetMapper, OmsTargetCostset>
  implements MOmsTargetCostsetService {

  @Autowired
  OmsTargetCostsetMapper omsTargetCostsetMapper;
/**
   * 7-1-07查询目标预算设置列表[8375]
   * gen by moon at 2/1/2024, 7:59:04 PM
   */
 @Trace(operationName = "7-1-07查询目标预算设置列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsTargetCostset>  queryTargetBudgetSetList(QueryTargetBudgetSetListReq reqDto) {
     
        try {
            QueryWrapper<OmsTargetCostset> queryWrapper = new QueryWrapper<>();
            
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCostset.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<OmsTargetCostset>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 7-1-07查询目标预算设置详情[8376]
   * gen by moon at 2/1/2024, 7:59:07 PM
   */
 @Trace(operationName = "7-1-07查询目标预算设置详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsTargetCostset  queryTargetBudgetSetDetail(QueryTargetBudgetSetDetailReq reqDto) {
     
        try {
         
            QueryWrapper<OmsTargetCostset> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetCostset> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsTargetCostset retData = retList.get(0);
                
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 7-1-07修改目标预算设置[8383]
   * gen by moon at 2/1/2024, 7:59:08 PM
   */
 @Trace(operationName = "7-1-07修改目标预算设置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateTargetBudgetSet(OmsTargetCostset omsTargetCostset) {
     omsTargetCostset.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsTargetCostset.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsTargetCostset);
  }
/**
   * 7-1-07修改目标预算设置（数据库加）[8384]
   * gen by moon at 2/1/2024, 7:59:09 PM
   */
 @Trace(operationName = "7-1-07修改目标预算设置（数据库加）")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateTargetBudgetSetData(OmsTargetCostset omsTargetCostset) {



     omsTargetCostset.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
     omsTargetCostset.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME

     UpdateWrapper<OmsTargetCostset> uw = new UpdateWrapper<>();
     uw.eq("target_cost_set_id",omsTargetCostset.getTargetCostSetId());
     if(omsTargetCostset.getCycleBudgetAmount()!=null){
         uw.setSql("cycle_budget_amount = cycle_budget_amount + " + omsTargetCostset.getCycleBudgetAmount());//ADD_NUMERICAL
     }
     omsTargetCostset.setCycleBudgetAmount(null);
     return super.update(omsTargetCostset,uw);
        
  }
/**
   * 7-1-07修改目标预算设置（数据库减）[8387]
   * gen by moon at 2/1/2024, 7:59:09 PM
   */
 @Trace(operationName = "7-1-07修改目标预算设置（数据库减）")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  reduceTargetBudgetSet(OmsTargetCostset omsTargetCostset) {
     omsTargetCostset.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
     omsTargetCostset.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME

     UpdateWrapper<OmsTargetCostset> uw = new UpdateWrapper<>();
     uw.eq("target_cost_set_id",omsTargetCostset.getTargetCostSetId());
     if(omsTargetCostset.getCycleBudgetAmount()!=null){
         uw.setSql("cycle_budget_amount = cycle_budget_amount - " + omsTargetCostset.getCycleBudgetAmount());//ADD_NUMERICAL
     }
     omsTargetCostset.setCycleBudgetAmount(null);
     return super.update(omsTargetCostset,uw);
  }
/**
   * 7-1-07批量查询目标预算设置[8398]
   * gen by moon at 2/1/2024, 7:59:12 PM
   */
 @Trace(operationName = "7-1-07批量查询目标预算设置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsTargetCostset>  batchQueryTargetBudgetSet(BatchQueryTargetBudgetSetReq reqDto) {
     
        try {
            QueryWrapper<OmsTargetCostset> queryWrapper = new QueryWrapper<>();
            
            if(reqDto.getTargetBudgetSetList()==null || reqDto.getTargetBudgetSetList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("eva_obj_target_cycle_id", reqDto.getTargetBudgetSetList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCostset.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<OmsTargetCostset>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 7-1-07新增目标预算设置[8374]
   * gen by moon at 2/4/2024, 8:05:22 PM
   */
 @Trace(operationName = "7-1-07新增目标预算设置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addTargetBudgetSet(OmsTargetCostset omsTargetCostset) {
     omsTargetCostset.setTargetCostSetId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsTargetCostset.setIsArchive("FALSE");
omsTargetCostset.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsTargetCostset.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsTargetCostset!=null && omsTargetCostset.getSubjectLifeCycle()!=null &&  omsTargetCostset.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetCostset.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(omsTargetCostset!=null && omsTargetCostset.getSubjectLifeCycle()!=null && omsTargetCostset.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsTargetCostset.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
          }
omsTargetCostset.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsTargetCostset.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsTargetCostset!=null && omsTargetCostset.getSubjectLifeCycle()!=null && omsTargetCostset.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             omsTargetCostset.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
if(omsTargetCostset!=null && omsTargetCostset.getSubjectLifeCycle()!=null && omsTargetCostset.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsTargetCostset.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
omsTargetCostset.setIsValid("FALSE");
omsTargetCostset.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
omsTargetCostset.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(omsTargetCostset)){
        return omsTargetCostset.getTargetCostSetId();
     }else {
        return "-1";
     }

  }
/**
   * 7-1-07批量删目标预算设置[8378]
   * gen by moon at 2/18/2024, 10:07:27 PM
   */
 @Trace(operationName = "7-1-07批量删目标预算设置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchDeleteTargetBudgetSet(List<String> reqDto) {

          if(reqDto==null || reqDto.size()==0){
            return false;
          }

          return super.removeByIds(reqDto);
  }
/**
   * 7-1-07查询目标预算设置By资金来源列表[8531]
   * gen by moon at 2/27/2024, 10:48:35 PM
   */
 @Trace(operationName = "7-1-07查询目标预算设置By资金来源列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsTargetCostset>  queryTargetBudgetSourceList(QueryTargetBudgetSourceListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCostset> queryWrapper = new QueryWrapper<>();

            if(reqDto.getTargetBudgetSourceList()==null || reqDto.getTargetBudgetSourceList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("capital_source_id", reqDto.getTargetBudgetSourceList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCostset.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCostset.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCostset>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 7-1-07批量改目标预算设置[9766]
   * gen by moon at 6/9/2024, 1:19:47 AM
   */
 @Trace(operationName = "7-1-07批量改目标预算设置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchUpdateTargetBudgetSet(List<OmsTargetCostset> omsTargetCostset) {

      if(omsTargetCostset==null || omsTargetCostset.size()==0){
        return false;
      }
        return super.saveOrUpdateBatch(omsTargetCostset);

  }
}
